package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.example.entity.Order;
import org.example.entity.OrderItem;
import org.example.mapper.OrderItemMapper;
import org.example.mapper.OrderMapper;
import org.example.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 订单服务实现类
 */
@Slf4j
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private OrderItemMapper orderItemMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean createOrder(Order order, List<OrderItem> orderItems) {
        try {
            // 设置创建时间
            order.setCreateTime(LocalDateTime.now());
            order.setUpdateTime(LocalDateTime.now());
            
            // 保存订单
            boolean orderSaved = this.save(order);
            
            if (orderSaved && orderItems != null && !orderItems.isEmpty()) {
                // 设置订单项的订单ID和创建时间
                for (OrderItem item : orderItems) {
                    item.setOrderId(order.getOrderId());
                    item.setUserId(order.getUserId());
                    item.setCreateTime(LocalDateTime.now());
                    item.setUpdateTime(LocalDateTime.now());
                }
                
                // 批量保存订单项
                for (OrderItem item : orderItems) {
                    orderItemMapper.insert(item);
                }
            }
            
            log.info("订单创建成功，订单ID: {}", order.getOrderId());
            return true;
        } catch (Exception e) {
            log.error("创建订单失败", e);
            throw new RuntimeException("创建订单失败", e);
        }
    }

    @Override
    public List<Order> getOrdersByUserId(Long userId) {
        return baseMapper.selectByUserId(userId);
    }

    @Override
    public List<Order> getOrdersByStatus(String orderStatus) {
        return baseMapper.selectByOrderStatus(orderStatus);
    }

    @Override
    public List<Order> getOrdersByUserIdAndStatus(Long userId, String orderStatus) {
        return baseMapper.selectByUserIdAndOrderStatus(userId, orderStatus);
    }

    @Override
    public Order getOrderDetail(Long orderId) {
        // 查询订单基本信息
        Order order = this.getById(orderId);
        if (order != null) {
            // 查询订单项
            List<OrderItem> orderItems = orderItemMapper.selectByOrderId(orderId);
            // 这里可以设置一个属性来存储订单项，或者创建一个VO类
            log.info("订单详情查询成功，订单ID: {}, 订单项数量: {}", orderId, orderItems.size());
        }
        return order;
    }

    @Override
    public boolean updateOrderStatus(Long orderId, String orderStatus) {
        LambdaUpdateWrapper<Order> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Order::getOrderId, orderId)
                    .set(Order::getOrderStatus, orderStatus)
                    .set(Order::getUpdateTime, LocalDateTime.now());
        
        boolean updated = this.update(updateWrapper);
        if (updated) {
            log.info("订单状态更新成功，订单ID: {}, 新状态: {}", orderId, orderStatus);
        }
        return updated;
    }
} 